package com.jemmyee.task.web.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.core.web.Constants;
import com.jemmyee.core.web.utils.MathUtils;
import com.jemmyee.task.entity.Task;
import com.jemmyee.task.web.TaskConstants;
import com.jemmyee.task.web.controller.kernel.BaseTaskAdminController;


/**
 * @Description: 后台task
 * @author:jemmyee@gmail.com
 * @date:2011-8-31
 * @version:v1.0
 */
@Controller("taskTaskAdminController")
@RequestMapping(Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_ADMIN+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_TASK)
public class TaskController extends BaseTaskAdminController{
	
	private static Log log= LogFactory.getLog(TaskController.class);
	/*分页查出保存的条件*/
	private static final String ENTITY_SESSION_BEAN="admin_session_bean_"+MathUtils.getRandomDouble();
	
	private static final String ROOT_VIEW =Constants.MV_URI_MAIN_ADMIN+Constants.MV_URI_APP_TASK+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_TASK;
	private static final String ROOT_URI=Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_ADMIN+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_TASK;

	@RequestMapping(Constants.MV_URI_COMM_DETAIL + "/{id}")
	public ModelAndView detail(@PathVariable String id) {
		ModelAndView mv = new ModelAndView();
		Task item = taskService.findById(id);
		initDetail(mv, item,ROOT_URI, ROOT_VIEW);
		return mv;
	}
	
	@RequestMapping(Constants.MV_URI_COMM_MODIFY + "/{id}/{attr}/{nval}")
	@ResponseBody
	public Map<String, Object> modify(@PathVariable String id,
			@PathVariable String attr, @PathVariable Integer nval,
			HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		String sql = "update"+TaskConstants.TABLE_HQL_NAME_TASK+" set " + attr + " =" + nval
				+ " where id='" + id+"'";
		// System.out.println("sql:"+sql);
		try {
			taskService.excuteSql(sql);
			model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_SUCC);
		} catch (Exception e) {
			model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_ERROR);
			log.error(e);
		}
		return model;
	}
	
	@RequestMapping(value = Constants.MV_URI_COMM_LIST
			+ "/{from}/{pageSize}/{startIndex}")
	public ModelAndView list(@PathVariable String from, @PathVariable Integer pageSize,
			@PathVariable Integer startIndex, @ModelAttribute Task bean,
			HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Task.class);
		ModelAndView mv = new ModelAndView();
		Task temp = new Task();
		if (from.equals(Constants.PAGE_ENTRY_TYPE_FORM)) {
			temp = bean;
			request.getSession().setAttribute(ENTITY_SESSION_BEAN, bean);
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_PAGE)) {
			if (request.getSession().getAttribute(ENTITY_SESSION_BEAN) != null) {
				temp = (Task) request.getSession().getAttribute(
						ENTITY_SESSION_BEAN);
			}
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_MENU)) {

		}

		if (pageSize == 0) {
			pageSize = Constants.DEFAULT_PAGESIZE;
		}
		if (bean.getBkw() != null && !bean.getBkw().equals("")) {
			dc.add(Restrictions.like("name", "%" + bean.getBkw() + "%"));
		}
		if (bean.getStartTime() != null && bean.getEndTime() != null) {
			dc.add(Restrictions.ge("addTime", bean.getStartTime()));
			dc.add(Restrictions.le("addTime", bean.getEndTime()));
		}
		if (bean.getStartTime() != null && bean.getEndTime() == null) {
			dc.add(Restrictions.ge("addTime", bean.getStartTime()));
		}
		if (bean.getStartTime() == null && bean.getEndTime() != null) {
			dc.add(Restrictions.le("addTime", bean.getEndTime()));
		}
		dc.addOrder(Order.desc("addTime"));
		Page page = taskService.findPageByCriteria(dc, pageSize, startIndex);
		if (pageSize != 0) {
			page.setPageSize(pageSize);
		}
		temp.setHightLight(Constants.HIGHTLIGHT_PREFIX + bean.getBkw()
				+ Constants.HIGHTLIGHT_SUFFIX);
		String pageURI=request.getContextPath()+Constants.SYM_PATH_SEPARATOR+ROOT_URI+Constants.MV_URI_COMM_LIST;
		initList(mv,page,Constants.MV_VIEW_LIST,pageURI,ROOT_URI,temp,ROOT_VIEW,-1,pageSize,startIndex);
		return mv;
	}

 
}
